package world

import (
	"context"
	"github.com/google/uuid"
	"github.com/topfreegames/pitaya"
	"github.com/topfreegames/pitaya/cluster"
	"github.com/topfreegames/pitaya/component"
	"github.com/topfreegames/pitaya/fairy/constants"
	"github.com/topfreegames/pitaya/fairy/manager"
	"github.com/topfreegames/pitaya/fairy/model"
	protos2 "github.com/topfreegames/pitaya/fairy/protos"
	"github.com/topfreegames/pitaya/logger"
	"math/rand"
	"time"
)

type HallDealer struct {
	component.Base
}

func NewHallDealer() *HallDealer {
	return &HallDealer{}
}

func (r *HallDealer) CheckPlayerName(ctx context.Context,req *protos2.LWCheckPlayerName) (*protos2.WLCheckPlayerNameRet,error) {
	b := manager.PlayerNameManager.CheckForAddNewName(*req.Name)
	exist := int32(0)
	if b {
		exist = 1
	}
	return &protos2.WLCheckPlayerNameRet{
		Name:      req.Name,
		IsExist:   &exist,
	},nil

}

func (r *HallDealer) AddNewPlayer(ctx context.Context,req *protos2.LWAddNewPlayer) (*protos2.RpcResponse,error) {
	sexId := 0
	if req.Sexid != nil {
		sexId = int(*req.Sexid)
	}
	manager.PlayerNameManager.UpdateNewRoleName(*req.Name,*req.AccountId,*req.PlayerGuId,sexId,*req.AppId)
	return &protos2.RpcResponse{RetCode:&constants.SuccessRet},nil
}

func (r *HallDealer) DeleteTmpPlayerName(ctx context.Context,req *protos2.GWDeleteTmpPlayerName) (*protos2.RpcResponse,error) {
	manager.PlayerNameManager.DeleteTmpRoleName(*req.Name)
	return &protos2.RpcResponse{RetCode:&constants.SuccessRet},nil
}

func (r *HallDealer) CheckLoginNum(ctx context.Context,req *protos2.LWCheckLoginNum) (*protos2.WLCheckLoginNumRet,error)  {
	maxPlayerNum := pitaya.GetConfig().GetInt("pitaya.game.worldserver.maxpoolplayersize")
	num := int32(maxPlayerNum - manager.OnlinePlayerManager.GetOnlinePlayerNum())
	return &protos2.WLCheckLoginNumRet{
		PlayerId: req.PlayerId,
		Num:      &num,
	},nil
}

func (r *HallDealer) GetGSInfo(ctx context.Context,req *protos2.LWGetGSInfo) (*protos2.WLGetGSInfoRet,error) {
	playerData := req.PlayerData
	logger.Log.Infof("[GetGSInfo] Begin accountId=%d,playerId=%d",playerData.AccountId,playerData.PlayerId)

	//随机选择一台网关 让客户端登录
	servers,err := pitaya.GetServersByType(constants.SvTypeGame)
	if err != nil {
		logger.Log.Errorf("GetGSInfo failed,error=%s",err.Error())
		return nil,err
	}
	srvList := make([]*cluster.Server, 0)
	s := rand.NewSource(time.Now().Unix())
	rnd := rand.New(s)
	for _, v := range servers {
		srvList = append(srvList, v)
	}
	server := srvList[rnd.Intn(len(srvList))]
	ticket := uuid.New().String()
	playerInfo := manager.OnlinePlayerManager.GetPlayerInfoByAccount(*playerData.AccountId,*playerData.AppId)
	if playerInfo == nil {
		playerInfo = &model.PlayerInfo{
			FullData:   playerData,
			Ticket:     ticket,
			ServerId:   server.ID,
		}
		manager.OnlinePlayerManager.AddPlayerInfo(*playerData.AccountId,*playerData.AppId,playerInfo)
	}

	id := int32(1)
	gsInfoRet := &protos2.WLGetGSInfoRet{
		Ticket:     &ticket,
		ServerInfo:&protos2.MsgServerInfo{
			Id:    &id,
			Ip:   &server.ID,
		},
	}
	return gsInfoRet,nil
}